package main

import (
	"example.com/m/baidu"
	"example.com/m/global"
	"example.com/m/uitls"
	"fmt"
	"github.com/go-vgo/robotgo"
	"log"
	"os"
	"strings"
	"time"
)

var (
	jump  = false
	close = make(chan int, 1)
)

func main() {
	var loc baidu.Locations
	var backup_loc baidu.Locations
	go CheckPid()
	go quit()
	go IsClose()
	//进行截图
	filename := Begin()
	if filename == "" {
		return
	}
	//ocr识别
	data := baidu.AccurateBasic("zawu/lalaimg.png")
	for _, v := range data.WordsResult {
		if find := strings.Contains(v.Words, "寻找对局"); find {
			loc = v.Location
		}
	}
	if loc.Height == 0 {
		log.Println("can't find params")
		return
	}
	// 左键
	robotgo.MoveClick(int(loc.Left), int(loc.Top), "left", true)

	time.Sleep(2 * time.Second)

	//寻找成功,有寻找成功的时间
	var stop bool
	//var temp int //计数器 防止有人点拒绝
	for !stop {
		filename1 := Begin()
		if filename1 == "" {
			log.Println("screen is fail! ")
			return
		}
		//ocr识别
		data1 := baidu.AccurateBasic("zawu/lalaimg.png")
		for _, v := range data1.WordsResult {
			if find := strings.Contains(v.Words, "接受"); find {
				backup_loc = v.Location
				break
			}
		}
		if backup_loc.Height == 0 {
			continue
		}
		for !jump {
			//一直点确认
			robotgo.MoveClick(int(backup_loc.Left), int(backup_loc.Top), "left", true)
			time.Sleep(2 * time.Second)
		}
		fmt.Println("thread is begin !")
		stop = true
	}
	//开始进入游戏界面

	for {
		var temp_mouse baidu.Locations
		filename2 := Begin()
		if filename2 == "" {
			break
		}
		//ocr识别
		data2 := baidu.AccurateBasic("zawu/lalaimg.png")
		for _, v := range data2.WordsResult {
			if find := strings.Contains(v.Words, "购买经验值"); find {
				temp_mouse = baidu.Locations{
					Top:    1041,
					Left:   486,
					Width:  0,
					Height: 0,
				}
				break
			}
		}
		if temp_mouse.Top == 0 {
			time.Sleep(5 * time.Second)
			continue
		}
		robotgo.MoveClick(int(temp_mouse.Left), int(temp_mouse.Top), "left", true)
	}

}

func Begin() (filename string) {
	bitmap := robotgo.CaptureScreen()
	defer robotgo.FreeBitmap(bitmap)
	img := robotgo.ToImage(bitmap)
	err := robotgo.Save(img, fmt.Sprintf("zawu/lalaimg.png"))
	if err != nil {
		fmt.Println("screen is ok")
		return ""
	}
	return "lalaimg.png"
}

func CheckPid() {
	var temp bool
	for !temp {
		_, temp = uitls.PidIsExist(global.LOL)
		time.Sleep(time.Second)
	}
	jump = true
}

func quit() {
	var q bool
	for !q {
		var temp_mouse baidu.Locations
		filename2 := Begin()
		if filename2 == "" {
			break
		}
		//ocr识别
		data2 := baidu.AccurateBasic("zawu/lalaimg.png")
		for _, v := range data2.WordsResult {
			if find := strings.Contains(v.Words, "现在退出"); find {
				temp_mouse = v.Location
				break
			}
		}
		if temp_mouse.Top == 0 {
			time.Sleep(5 * time.Second)
			continue
		}
		robotgo.MoveClick(int(temp_mouse.Left), int(temp_mouse.Top), "left", true)
		q = true
	}
	time.Sleep(5 * time.Second)
	_, is := uitls.PidIsExist(global.LOL)
	if !is {
		fmt.Println("exe is close ")
		return
	} else {
		fmt.Println("quit fail !")
		close <- 1
	}
	fmt.Println("quit the game ")
}

func IsClose() {
	for {
		select {
		case t := <-close:
			if t == 1 {
				os.Exit(1)
			}
		default:
			time.Sleep(5 * time.Second)
		}
	}
}
